<?php defined('BASE_PATH') or die('No direct script access.');
/*Author : Gialac
 * Class : Loader
*/
class Loader {
    protected $registry;
    public function __construct($registry) {
        $this->registry = $registry;
    }
    // function load model
    function model($path) {
        // Kiểm tra đường dẫn có tồn tại không
        $x = explode('/',$path);
        $module =$x[0];
        $model = end($x);
        $path=MODULE.$module.'/models/'.$model.'.php';
        if(is_file($path)==FALSE) {
            echo 'Invalid file path: `' . $path . '`';
            exit ();
        }
        include $path;
        $this->registry->$model = new $model($this->registry);
        unset($path,$model,$module,$x);
    }
    /// function load helper
    function helper($path) {
        $path=str_replace('_','/',$path);
        $path=HELPER.$path.'.php';
        if(is_file($path)==FALSE) {
            echo 'Invalid file path: `' . $path . '`';
            exit ();
        }
        include $path;
        unset ($path);
    }
    // function load libraries
    function library($path) {
        // Kiểm tra đường dẫn có tồn tại không
        $name=$path;
        $x = explode('/',$path);
        $library = end($x);
        $path=str_replace('_','/',$path);
        $path=LIBRARY.$path.'.php';
        if(is_file($path)==FALSE) {
            echo 'Invalid file path: `' . $path . '`';
            exit ();
        }        
        include $path;
        $this->registry->$name= new $library();
        unset($path,$name,$x,$library);
    }
}
// End of class loader